package com.zhiyou100.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.model.HospitalInfor;
import com.zhiyou100.model.RegistrationInfor;
import com.zhiyou100.service.HouInforService;

@Controller
public class HosInforController {
	
	@Autowired
	private HouInforService hifService;
	@RequestMapping("/hosInfor/list")
	public String hosInforList(Model model,
			@RequestParam(defaultValue="")String medicalRecord,
			@RequestParam(defaultValue="")String doctorName,
			@RequestParam(defaultValue="")String sectionName,
			@RequestParam(defaultValue="1")int pageNo) {
		Map<String,Object> map = new HashMap<>();
		map.put("medicalRecord", medicalRecord);
		map.put("doctorName", doctorName);
		map.put("sectionName", sectionName);
		
		// 分页
		PageHelper.startPage(pageNo, 3);
		// 调用服务层
		List<HospitalInfor> lists = hifService.findAll(map);
		System.out.println("遍历得到的lists: "+lists);
		PageInfo<HospitalInfor> pageInfo = new PageInfo<>(lists);
		model.addAttribute("lists", pageInfo.getList());
		model.addAttribute("map", map);
		model.addAttribute("page", pageInfo);
		return "forward:/view/hospital/hosList.jsp";
	}
	
	// 详情
	@RequestMapping("/hosInfor/detail")
	public String hosInforDetail(int medical_record,Model model) {
		HospitalInfor hos = hifService.findById(medical_record);
		model.addAttribute("hos", hos);
		return "forward:/view/hospital/hosDetail.jsp";
		
	}
	// 添加
	@RequestMapping(value="/hosInfor/add",method=RequestMethod.GET)
	public String addHospital() {
		return "forward:/view/hospital/hosAdd.jsp";
	}
	@RequestMapping(value="/hosInfor/add",method=RequestMethod.POST)
	public String addHospital2(HospitalInfor hos) {
		hifService.addHospital(hos);
		hifService.addHospital2(hos);
		
		System.out.println("住院添加的controller获取到的hos: "+hos);
		return "forward:/hosInfor/list";
	}
	// 更新 查询
	@RequestMapping(value="/hosInfor/edit",method=RequestMethod.GET)
	public String edithospital(int medical_record,Model model) {
		HospitalInfor hif = hifService.findToEdit(medical_record);
		System.out.println("更新查询从页面获取的id: "+medical_record);
		System.out.println("更新查询从数据库获取的数据: "+hif);
		model.addAttribute("hif", hif);
		return "forward:/view/hospital/hosEdit.jsp";
		
	}
	// 更新 更改
	@RequestMapping(value="/hosInfor/edit",method=RequestMethod.POST)
	public String updateHospital(HospitalInfor hos) {
		System.out.println("更新从页面接受到的值: "+hos);
		// 调用服务层
		hifService.editHospital(hos);
		hifService.editHospital2(hos);
		return "forward:/hosInfor/list";
		
	}
	// 退院 删除
	@RequestMapping("/hosInfor/delete")
	public String deleteHospital(int medical_record ) {
		hifService.deleteById(medical_record);
		return "forward:/hosInfor/list";
		
	}
	// 出院,改状态
	@RequestMapping("/hosInfor/update")
	public String upHospital(int medical_record) {
		hifService.updateById(medical_record);
		return "forward:/hosInfor/list";
	}
}
